<%--
  Created by IntelliJ IDEA.
  User: 华为
  Date: 2025/3/14
  Time: 16:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.23/dist/css/layui.css" rel="stylesheet">
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">患者姓名</label>
        <div class="layui-input-block">
            <input type="text" name="patientName"  id="patientName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号码</label>
        <div class="layui-input-block">
            <input type="text" name="phoneNumber" id="phoneNumber" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">出生日期</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="birthDate" name="birthDate" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">身份证</label>
        <div class="layui-input-block">
            <input type="text" name="idCard" id="idCard" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-block">
            <input type="text" name="age" id="age" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">
            性别
        </label>
        <div class="layui-input-block">
            <select name="sex"  id="sex" lay-filter="parkId">
                <option value="">请选择</option>
                <option value="0">女</option>
                <option value="1">男</option>
                <option value="2">保密</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">就诊日期</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="createTime" name="createTime" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">就诊类型</label>
        <div class="layui-input-block">
            <input type="radio" name="registrationType" value="0" title="初诊" checked>
            <input type="radio" name="registrationType" value="1" title="复诊">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">科室</label>
        <div class="layui-input-block">
            <select name="departmentId"  id="departmentId" lay-filter="parkId">
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">医生</label>
        <div class="layui-input-block">
            <select name="uid"  id="uid" lay-filter="monthRulesId">
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
    <button type="button" class="layui-btn" id="ID-upload-demo-btn">
        <i class="layui-icon layui-icon-upload"></i> 头像
    </button>
    <div style="width: 132px;">
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="ID-upload-demo-img" style="width: 100%; height: 92px;">
            <div id="ID-upload-demo-text"></div>
        </div>
        <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="filter-demo">
            <div class="layui-progress-bar" lay-percent=""></div>
        </div>
        <input type="hidden" name="imgUrl" id="imgUrl">
    </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="//unpkg.com/layui@2.9.23/dist/layui.js"></script>
<script src="/static/js/jquery-1.8.3.js"></script>
<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;
        var upload = layui.upload;
        var layer = layui.layer;
        var element = layui.element;
        var $ = layui.$;
        // 单图片上传
        var uploadInst = upload.render({
            elem: '#ID-upload-demo-btn',
            url: '/upload/img', // 实际使用时改成您自己的上传接口即可。
            before: function(obj){
                // 预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                });

                element.progress('filter-demo', '0%'); // 进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function(res){
                // 若上传失败
                if(res.code !=200){
                    return layer.msg('上传失败');
                }
                // 上传成功的一些操作
                // …
                $("#imgUrl").val(res.data)
                $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
            },
            error: function(){
                // 演示失败状态，并实现重传
                var demoText = $('#ID-upload-demo-text');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            },
            // 进度条
            progress: function(n, elem, e){
                element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });
        $("#patientName").change(function () {
            let patientName = $("#patientName").val();
            $.ajax({
                url:"/selectPatientName" ,
                type: "post",
                data: {patientName:patientName},
                dataType: "json",
                success(res) {
                    // layer.alert(res.data)
                    if(res.code===200){
                        $("#phoneNumber").val(res.data.userPhone)
                        $("#birthDate").val(res.data.birthDate)
                        $("#idCard").val(res.data.idCard)
                        $("#age").val(res.data.age)
                        $("#sex").val(res.data.sex)
                        form.render(); // 仅渲染 select 元素

                    }
                },
                error() {

                }
            })
        })
        $("#idCard").change(function () {
            let idCard=$("#idCard").val();
            $.ajax({
                url: "/selectIdCard",
                type: "post",
                data: {idCard:idCard},
                dataType: "json",
                success(res) {
                  if(res.code===200){
                      $("#age").val(res.data)
                  }
                },
                error() {

                }
            })
        })
        getDoctor()
        function getDoctor() {
            $.ajax({
                url:"/getDoctor" ,
                dataType: "json",
                success(res) {
                    if(res.code==200){
                        for (let doctor of res.data) {
                            $("#uid").append(`
                           <option value="\${doctor.userId}">\${doctor.userName}</option>
                        `)
                        }
                        form.render(); // 仅渲染 select 元素

                    }
                    else{
                        layer.alert(res.msg)
                    }
                },
                error() {
                    layer.alert('服务器异常')
                }
            })
        }
        getDep()
        function getDep() {
            $.ajax({
                url:"/getDep" ,
                dataType: "json",
                success(res) {
                    if(res.code==200){
                        for (let dep of res.data) {
                            $("#departmentId").append(`
                           <option value="\${dep.departmentId}">\${dep.departmentName}</option>
                        `)
                        }
                        form.render(); // 仅渲染 select 元素

                    }
                    else{
                        layer.alert(res.msg)
                    }
                },
                error() {
                    layer.alert('服务器异常')
                }
            })
        }
        // getSex()
        // function getSex() {
        //     $.ajax({
        //         url:"/getSex" ,
        //         dataType: "json",
        //         success(res) {
        //             if(res.code==200){
        //                 for (let sex of res.data) {
        //                     $("#sex").append(`
        //                    <option value="\${sex.dictId}">\${sex.dictName}</option>
        //                 `)
        //                 }
        //                 form.render(); // 仅渲染 select 元素
        //
        //             }
        //             else{
        //                 layer.alert(res.msg)
        //             }
        //         },
        //         error() {
        //             layer.alert('服务器异常')
        //         }
        //     })
        // }
        // 日期
        laydate.render({
            elem: '#birthDate',
            type:'datetime',
            fullPanel: true // 2.8+
        });
        laydate.render({
            elem: '#createTime',
            type:'datetime',
            fullPanel: true // 2.8+
        });
        // 自定义验证规则
        form.verify({
            pass: function(value) {
                if (!/(.+){6,12}$/.test(value)) {
                    return '密码必须 6 到 12 位';
                }
            }
        });

        // 指定开关事件
        form.on('switch(switchTest)', function(data){
            layer.msg('开关 checked：'+ (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是 ON|OFF', data.othis)
        });

        // 提交事件
        form.on('submit(demo1)', function(data){
            var field = data.field; // 获取表单字段值
            // 显示填写结果，仅作演示用
            layer.alert(JSON.stringify(field), {
                title: '当前填写的字段值'
            });
            // 此处可执行 Ajax 等操作
            // …
            $.ajax({
                url: "/registration/getAdd",
                type: "post",
                data: JSON.stringify(field),
                contentType:"application/json" ,
                dataType: "json",
                success(res) {
                  if(res.code==200){
                      layer.alert(res.msg)
                      location.href="/registration/toList"
                  }
                },
                error() {

                }
            })
            return false; // 阻止默认 form 跳转
        });

        // 日期
        laydate.render({
            elem: '#date'
        });

        // 普通事件
        util.on('lay-on', {
            // 获取验证码
            "get-vercode": function(othis){
                var isvalid = form.validate('.demo-phone'); // 主动触发验证，v2.7.0 新增
                // 验证通过
                if(isvalid){
                    layer.msg('手机号规则验证通过');
                    // 此处可继续书写「发送验证码」等后续逻辑
                    // …
                }
            }
        });
    });
</script>

</body>
</html>
